﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

namespace AMS.Inventory.PDA.Helper
{
    public class FormRuler
    {
        private readonly ObjXmlReader m_XmlReader;
        public static string m_DbRule = "db_rule", m_Only = "唯一性", m_Exists = "存在性", m_IsInt = "整数",m_Need="必填",m_IengthErg="长度超出范围 ";
        public static string TYPE_NAME = "TypeName", ERR_MSG = "ErrMsg", REG_EXP_VALUE = "RegExpValue", FORMAT_RULE = "FormatRule", LENGTH = "length";
        public FormRuler()
		{
			m_XmlReader = new ObjXmlReader(ModCommon.PrjPath+"\\Resources\\FormatRuleTypeSetting.xml");
		}

        public string CheckProValByFormatRule(string proVal,string ruleTypeName)
        {
            string strRegExpVal = GetFormRuleRegByTypeName(ruleTypeName);
            string strErrMsg = GetFormRuleErrMsgByTypeName(ruleTypeName);
            if (ruleTypeName == m_Need && (proVal == null || proVal.Length == 0))
            {
                return strErrMsg;   
            }
            if (String.IsNullOrEmpty(proVal))
                return "";
            Regex reg = new Regex(strRegExpVal);
            if (!reg.IsMatch(proVal))
                return strErrMsg;
            return "";
        }

        public string GetFormRuleRegByTypeName(string typeName)
        {
            string val = "";
            StringBuilder sbReVal = new StringBuilder();
            if (typeName == null || typeName.Length == 0)
                return val;

            XmlNode nd = m_XmlReader.GetXmlNode("setting/" + typeName);
            if (nd == null)
                return val;
            val = m_XmlReader.GetNodeAttributeValue(nd, REG_EXP_VALUE, true);

            return val;
        }

        public string GetFormRuleErrMsgByTypeName(string typeName)
        {
            string val = "";
            StringBuilder sbReVal = new StringBuilder();
            if (typeName == null || typeName.Length == 0)
                return val;

            XmlNode nd = m_XmlReader.GetXmlNode("setting/" + typeName);
            if (nd == null)
                return val;

            val = m_XmlReader.GetNodeAttributeValue(nd, ERR_MSG, true);

            return val;
        }
    }
}
